package com.j1ay.forum.controller;

import com.j1ay.forum.model.pojo.po.Permission;
import com.j1ay.forum.model.dto.params.PageParams;
import com.j1ay.forum.service.PermissionService;
import com.j1ay.forum.model.pojo.vo.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * @Author J1ay
 * @Date 2021/12/18 16:07
 * @Description 权限管理
 */

@RestController
@RequestMapping("api/permission")
@Api(value="Permission Controller",tags={"权限管理类接口"})
public class PermissionController {

    @Autowired
    private PermissionService permissionService;

    @PreAuthorize("hasAnyAuthority('is:admin')")
    @ApiOperation("分页查看权限列表")
    @PostMapping("list")
    public Result getPermissionList(@RequestBody PageParams pageParams) {
        return permissionService.getPermissionList(pageParams);
    }

    @ApiOperation("增加权限")
    @PreAuthorize("hasAnyAuthority('is:admin', 'permission:self:update')")
    @PostMapping("add")
    public Result add(@RequestBody Permission permission){
        return permissionService.add(permission);
    }

    @ApiOperation("修改权限")
    @PreAuthorize("hasAnyAuthority('is:admin', 'permission:self:update')")
    @PutMapping("update")
    public Result update(@RequestBody Permission permission){
        return permissionService.update(permission);
    }

    @ApiOperation("删除权限")
    @PreAuthorize("hasAnyAuthority('is:admin', 'permission:self:update')")
    @DeleteMapping("delete/{id}")
    public Result delete(@PathVariable("id") Long id){
        return permissionService.delete(id);
    }
}
